﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace WPF_StringToDoubleConverter_Demo
{
    public class StringToDoubleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value is double doubleValue ? doubleValue.ToString(culture) : string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string strValue)
            {
                if (double.TryParse(strValue, NumberStyles.Any, culture, out double result))
                {
                    return result;
                }
                else if (string.IsNullOrEmpty(strValue))
                {
                    return 0.0; // 或者可以返回 null 视你的需求而定
                }
            }
            return 0.0; // 默认返回值
        }
    }
}
